package com.album.task;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

import com.album.http.FormFile;
import com.album.http.HttpRequest;
import com.album.utils.Config;
import com.album.utils.MyLog;

public class UploadImageTask extends Task{
	private String TAG = "UploadImageTask";
	private Context ctx;
	private ICallBack callBack;
	private String userName; //用户名
	private String title;
	private String type;
	private String public1;
	private String filePath;
	public UploadImageTask(Context ctx,String userName,String title,String type,String public1,String filePath,ICallBack resTask){
		this.ctx = ctx;
		this.callBack = resTask;
		this.userName = userName;
		this.title= title;
		this.type = type;
		this.public1 = public1;
		this.filePath = filePath;
	}

	public String setRequest(){
	    //18926128667@189.cn/
		userName = userName.substring(0,userName.lastIndexOf("@"));
		String value = "/tmp/"+userName+"/";
		MyLog.info(getClass(), "上传图片："+value);
		Map<String, String> params = new HashMap<String, String>();
		params.put("saveFilePath", value);
		params.put("title", title);
		params.put("type", type);
		params.put("public", public1);
		String http = Config.TyHttp+Config.TyUpHttp;
		String jsonString = null;
		try {
			File file = new File(filePath);
			String fileName = "pic";
			if (filePath != null && filePath.length() != 0) {
				fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
			}
			FormFile formfile = new FormFile(fileName, file, "file", "application/octet-stream");
		    jsonString = HttpRequest.uplaodPost(ctx,http, params, formfile,callBack);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonString;
	}
	@Override
	public void doTask(Task thread){
		Thread t = new Thread(thread);
		t.start();
	}
	@Override
	public void run() {
		//网络任务
		try {
			String strRsp = setRequest();
			callBack.CallBack(strRsp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

